﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace woolbrush
{
    public class MyWebAdapt
    {
        WebBrowser myBrowser = null;
        string loc = null;
        ListBox cList = null;
        bool bPrompt = false;

        public ~MyWebAdapt()
        {
            myBrowser.Dispose();

        }
        public MyWebAdapt(ListBox cList,string loc, bool bPrompt)
        {
            myBrowser = new WebBrowser();
            myBrowser.Visible = false;
            if (bPrompt)
            {
                myBrowser.Navigating += new WebBrowserNavigatingEventHandler(this.myBrowser_Navigating);
                myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.myBrowser_DocumentCompleted);
            }
            this.cList = cList;
            this.bPrompt = bPrompt;
            this.loc = loc;
        }
        public void StartNavigate()
        {
            myBrowser.Navigate(this.loc, false);
        }
        public void StartNavigate(string loc)
        {
            this.loc = loc;
            this.StartNavigate();
        }
        public void myBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.cList.Items.Clear();
            DateTime now = new DateTime();
            string txt = now.ToString() + e.Url.ToString() + ": start navigating...";
            this.cList.Items.Add((object)txt);
        }
        private void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            string doctxt = null;
            HtmlDocument hd = this.myBrowser.Document;
            if (hd == null) return;
            HtmlElement he = hd.GetElementById("id68644808");
            if (he == null)
            {
                doctxt = e.Url.ToString() + ": fail.";
                this.cList.Items.Add((object)doctxt);
                return;
            }
            doctxt = e.Url.ToString() + ": loaded.";
            this.cList.Items.Add((object)doctxt);

            doctxt = he.InnerText;

            string[] seperator = { ";" };
            string[] locations = doctxt.Trim().ToLower().Split(seperator, StringSplitOptions.RemoveEmptyEntries);
            this.cList.Items.AddRange((object[])locations);

        }
    }
}
